/**
 * Author: 刘发毅.
 * Time: 2016/11/28 0019
 */
angular.module('service.privilege', [
        "ngResource"
    ])
    .service('PrivilegeService', PrivilegeService);
PrivilegeService.$inject = ['$resource'];
function PrivilegeService($resource) {
// 根据活动ID读取活动内容单个
    this.getPromotionByPromotionId = function(promotionId){
        // 根据活动ID从服务器上获取一个活动内容
        var getActivity = $resource(privilege+'promotion/:promotionid');
        return  getActivity.get({promotionid:promotionId});
    };
    //根据店铺id获取店铺有效活动
    this.getPromotionByShopId = function(shopid){
        var ShopActivity = $resource(privilege+'promotion/shopid/effective/:shopid');
        return ShopActivity.query({shopid:shopid,status:0});
    };
    // 根据订单计算出优惠活动可享受的优惠列表
    this.calcPromotion = function(goodscontent, promotion_list) {
        var finallydiscount = 0;
        var total = goodscontent.totalprice;
        for (var i=0; i<promotion_list.length; i++) {
            var promotion = promotion_list[i];
            if(promotion.type){
                var discount = filtOneActivity(goodscontent,promotion.promotionRules,promotion.type);
                var currentlydiscount = 0;
                if(promotion.type==1){
                    currentlydiscount = discount.discount;
                    if(currentlydiscount>finallydiscount){
                        goodscontent.promotionid = promotion.promotionid;
                        goodscontent.promotionType = promotion.type;
                        goodscontent.promotionFull = discount.full;
                        finallydiscount  = currentlydiscount;
                    }
                }else if(promotion.type==2&&discount.discount!=0){
                    currentlydiscount = goodscontent.totalprice*(1-discount.discount/10);
                    if(currentlydiscount>finallydiscount){
                        goodscontent.promotionid = promotion.promotionid;
                        goodscontent.promotionType = promotion.type;
                        goodscontent.discountIntensity = discount.discount;
                        goodscontent.promotionFull = discount.full;
                        finallydiscount  = currentlydiscount;
                    }
                }
            }
        }
        goodscontent.promotionDiscount = finallydiscount;
        goodscontent.paymentprice = total-finallydiscount;
        return goodscontent;
    };
    //筛选可享受的活动
    function filtOneActivity(goodscontent, promotionRules,type) {
        var result = {
            discount:0,
            promotionid:''
        };
        var total = goodscontent.totalprice;
        if(type==1){
            promotionRules = promotionRules.sort(compare("discount"));
        }else if(type==2){
            promotionRules = promotionRules.sort(compareopposite("discount"));
        }
        //将优惠力度从小到大排序
        for (var i=0; i<promotionRules.length;i++) {
            var Rule = promotionRules[i];
            // 判断总金额条件
            if(type==1){
                if (Rule.full > total||Rule.discount>=total) {
                    continue;
                }
            }else if(type==2){
                if (Rule.full > total) {
                    continue;
                }
            }
            // 判断总金额条件
            result=Rule;
        }
        return result;
    }
    //从小到大排序
    function compare(property) {//比较函数
        return function (x,y){
            if (x[property] < y[property]) {
                return -1;
            } else if (x[property] > y[property]) {
                return 1;
            } else {
                return 0;
            }
        }
    }
    //从大到小排序
    function compareopposite(property) {//比较函数
        return function (x,y){
            if (x[property] > y[property]) {
                return -1;
            } else if (x[property] < y[property]) {
                return 1;
            } else {
                return 0;
            }
        }
    }
}


